package org.nowcoder.leetcode.slidingWindow;

/**
 * Title  : 2760. Longest Even Odd Subarray With Threshold
 * Source : https://leetcode.cn/problems/longest-even-odd-subarray-with-threshold/
 * Author : XrazYang
 * Date   : 2023-07-25
 */

public class LeetCode_2760 {
    public int longestAlternatingSubarray(int[] nums, int threshold) {
        //边界条件
        int left, right = 0;
        int len = 0;

        //初始化
        for (left = 0; left < nums.length; left++) {
            int temp = nums[left];
            if (temp % 2 == 0 && temp <= threshold) {
                len = 1;
                break;
            }
        }
        right = left + 1;
        while (right < nums.length) {
            int temp = nums[right];
            //符合条件
            if ((temp % 2 != nums[right - 1] % 2) && temp <= threshold) {
                len = Math.max(len, right - left + 1);
            } else {
                //移动left
                left = right;
                while (right < nums.length) {
                    if (nums[right] % 2 == 0 && nums[right] <= threshold) {
                        left = right;
                        break;
                    }
                    right++;
                }
            }
            right++;
        }
        return len;
    }
}
